package com.example.pwj.ffstudy.util;

import android.annotation.SuppressLint;

import com.blankj.utilcode.util.TimeUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by pwj on 2017/8/23.
 */

public class DateUtil {
    /**
     *
     * @param dateCount 距今日的天数。0取今日,1取昨天....
     * @return 20170823这种格式的日期
     */
    @SuppressLint("SimpleDateFormat")
    public static String getFormerDate(int dateCount) {
        return getFormerDate(dateCount, "yyyyMMdd");
    }
    @SuppressLint("SimpleDateFormat")
    public static String getFormerDate(int dateCount,String dateformatter) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(dateformatter);
        Date nowDate = new Date();
        Calendar calendar = Calendar.getInstance();
        Date formerDate = null;
        calendar.setTime(nowDate);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - dateCount);
        try {
            formerDate = dateFormat.parse(dateFormat.format(calendar.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateFormat.format(formerDate);
    }

    /**
     * 返回格式为:08月24日 星期四
     * @param dateCount
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDateAndWeek(int dateCount) {
        String date = getFormerDate(dateCount, "MM月dd日");
        String formerDate = getFormerDate(dateCount);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String week = TimeUtils.getChineseWeek(formerDate, dateFormat);
        week = week.replaceAll("周", "星期");
        return date + " " + week;
    }
}
